<?php

	class ShopsController extends AppController {
		var $name = 'Shops';
		var $uses = array('Shop');
		
		var $publicAction = array('index', 'available');

		function index() {
			$this->helpers[] = 'Number';
			
			// Frontpage
			$front_page = $this->Shop->Post->find(
				'first',
				array(
					'conditions' => array(
						'Post.shop_id' => Configure::read('Shop.id'),
						'Post.type' => 'pages',
						'Post.front_page' => 1
					)
				)
			);
			
			// Products
			$this->paginate = array(
				'conditions' => array(
					'Product.shop_id' => Configure::read('Shop.id'),
					'Product.deleted' => 0,
					'Product.disabled' => 0
				),
				'fields' => array(
					'Product.id',
					'Product.price',
					'Product.preview',
					'CONCAT(\''. Configure::read('Minicatalogo.url') . 'image/product/' . Configure::read('Shop.alias') . '/' .'\', REPLACE(Product.preview,\'.\',\'_preview.\')) AS preview',
					//
					'ProductDescription.name',
					'ProductDescription.slug',
					//
					'Currency.code',
					'Currency.symbol_left',
					'Currency.symbol_right',
					'Currency.decimal_places',
					'Currency.decimals',
					'Currency.thousands'
				),
				'contain' => array(
					'Currency',
					'ProductDescription'
				),
				'limit' => 25,
				'order' => 'Product.created DESC'
			);
			
			$products = $this->paginate('Product');
			
			$this->set(compact('products', 'front_page'));
		}
				
		function available() {
			$this->autoRender = false;
			$request = array();
			if ($this->RequestHandler->isAjax()) {
				$request['status'] = 'failed';
				if (!empty($this->data['Shop']['slug'])) {
					
					App::import('Vendor', 'functions');
					$slugNotClean = $this->data['Shop']['slug'];
					$this->data['Shop']['slug'] = slug($slugNotClean);

					if (preg_match('/^[0-9]+/', $slugNotClean)) {
						$request['message'] = __('La dirección no debe empezar con numeros.', true);
					}
					else if (!preg_match('/[a-zA-Z0-9_-]/', $slugNotClean)) {
						$request['message'] = __('La dirección no debe contener caracteres con acentos ni especiales.', true);
					}
					else if (in_array($this->data['Shop']['slug'], Configure::read('Shops.reservedWords')) OR $this->Shop->hasAny(array('Shop.slug' => $this->data['Shop']['slug']))) {
						$request['message'] = __('Esta dirección ya esta siendo usada por otro usuario. Por favor, elige otro nombre.', true);
					}
					else {
						$request['status'] = 'success';
					}
					
				} else {
					$request['message'] = __('Completa la dirección con un nombre que recuerden tus clientes.', true);
				}
				
				echo json_encode($request);
			} else {
				$this->redirect(array('controller' => 'home'));
			}
		}

		function dashboard_index() {
			$this->pageTitle = __('Panel', true);
		}
		
		function dashboard_settings() {
			$this->pageTitle = __('Información de tu tienda', true);
			
			if (!empty($this->data)) {
				if ($this->Shop->save($this->data)) {
					$this->Session->setFlash(__('La información de la tienda ha sido modificada exitosamente.', true), null, array('class' => 'success'));
					$this->redirect(array('controller' => 'shops', 'action' => 'dashboard_settings'));
				}
			}

			$this->data = $this->Shop->find(
				'first',
				array(
					'conditions' => array(
						'Shop.id' => Configure::read('Shop.id')
					),
					'fields' => array(
						
					),
					'recursive' => -1
				)
			);
			
			$industries = $this->Shop->IndustryShop->find(
				'all',
				array(
					'fields' => array(
						'id'
					),
					'contain' => array(
						'Industry' => array(
							'fields' => array(
								'id',
								'slug',
								'name'
							)
						)
					),
					'recursiver' => 0
				)
			);
			
			$this->set(compact('industries'));
			
			$this->set('currencies', $this->Shop->Currency->find('list', array('fields' => array('id', 'name'))));
			$this->set('countries', $this->Shop->Country->find('list'));
		}
		
		function dashboard_notifications() {
			$this->pageTitle = __('Notificaciones', true);
		}
	}

?>